﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using Common;

public partial class system_rolessystemcatalogsettings : PageBase
{
    public string RightItems = "";
    public int CategoryCount = 0;
    public string PageTitle = "Settings";

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;

        PageBegin("system role admin");

        RightItems = SystemCatalog();

        //保存
        if (Common.RequestBase.GetFormString("__EVENTTARGET").Trim().ToLower() == "saveinfo")
        {
            SaveSettings(Common.RequestBase.GetFormString("txtCatalogId").Trim());
        }
    }

    #region 目录显示
    string SystemCatalog()
    {
        StringBuilder _divHTML = new StringBuilder();
        string areaHTML = "<table class=\"sectionArea\" cellspacing=\"0\" cellpadding=\"0\" onclick=\"ProcessClick();\"><colgroup><col width=\"80px;\" /><col width=\"60px;\" style=\"text-align: center;\" /><col /></colgroup><tbody>{0}</tbody></table>";
        string catalogHTML = "<tr><td class=\"ms-crm-Form-Section ms-crm-Form-SectionBar\" >{6}</td><td class=\"ms-crm-Form-Section ms-crm-Form-SectionBar\"><img class=\"i\" id=\"roles_{0}\" alt=\"\" p=\"{1}\" otn=\"\" m=\"1\" x=\"16\" d=\"{2}\" b=\"{3}\" l=\"17\" linenum=\"{4}\" ischange=\"0\" name=\"{5}\"></td><td class=\"ms-crm-Form-Section ms-crm-Form-SectionBar\">&nbsp;</td></tr>{7}<tr height=\"10\"><td></td></tr>";

        StringBuilder _trHTML = new StringBuilder();
        DataTable dt = BLL.CommonBLL.Select("System_CatalogBase", "CatalogId, CatalogName, ParentId, Layer", " ORDER BY [Sort] ASC");
        int i = 0;
        foreach (DataRow dr in dt.Select(" Layer = 1"))
        {
            _trHTML.Append(string.Format(catalogHTML, dr[0].ToString(), i.ToString(), GetD(dr["CatalogId"].ToString()), dr["CatalogId"].ToString(), "1", dr["ParentId"].ToString(), dr[1].ToString(), GetItmes(dt, dr[0].ToString())));
            i++;
        }

        dt.Dispose();
        _divHTML.Append(string.Format(areaHTML, _trHTML.ToString()));
        return _divHTML.ToString();
    }

    string GetItmes(DataTable dat, string parentId)
    {
        string title = "<tr><td class=\"catalogNoteTitle\">{0}</td>";
        string items = "<td class=\"catalogNoteItems\"><img class=\"i\" id=\"roles_{0}\" alt=\"\" p=\"{1}\" otn=\"\" m=\"1\" x=\"16\" d=\"{2}\" b=\"{3}\" l=\"17\" linenum=\"{4}\" ischange=\"0\" name=\"{5}\"></td><td class=\"catalogChildNote\" valign=\"top\">{6}</td></tr>";

        StringBuilder _trHTML = new StringBuilder();
        StringBuilder _itemHTML = new StringBuilder();

        int i = 0;
        foreach (DataRow dr in dat.Select(" ParentId = " + parentId))
        {
            _itemHTML.Append(string.Format(title, dr["CatalogName"].ToString()));
            _itemHTML.Append(string.Format(items, dr[0].ToString(), i.ToString(), GetD(dr["CatalogId"].ToString()), dr["CatalogId"].ToString(), "1", parentId, GetChildNoteItmes(dat, dr[0].ToString())));
            i++;
        }

        return _itemHTML.ToString();
    }

    string GetChildNoteItmes(DataTable dat, string parentId)
    {
        string HTML = "<table style=\"table-layout: fixed;\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\"><tbody><tr>{0}</tr></tbody></table>";
        string title = "<td class=\"title\">{0}</td>";
        string items = "<td class=\"items\"><img class=\"i\" id=\"roles_{0}\" alt=\"\" p=\"{1}\" otn=\"\" m=\"1\" x=\"16\" d=\"{2}\" b=\"{3}\" l=\"17\" linenum=\"{4}\" ischange=\"0\" name=\"{5}\"></td>";

        StringBuilder _trHTML = new StringBuilder();
        StringBuilder _itemHTML = new StringBuilder();

        int i = 0;
        foreach (DataRow dr in dat.Select(" ParentId = " + parentId))
        {
            _itemHTML.Append(string.Format(title, dr["CatalogName"].ToString()));
            _itemHTML.Append(string.Format(items, dr[0].ToString(), i.ToString(), GetD(dr["CatalogId"].ToString()), dr["CatalogId"].ToString(), "1", parentId));
            i++;
        }

        if (_itemHTML.ToString() != "")
            return string.Format(HTML, _itemHTML.ToString());
        else
            return "&nbsp;";
    }

    /// <summary>
    /// 获取用户目录状态
    /// </summary>
    /// <param name="CatalogId"></param>
    /// <returns></returns>
    public int GetD(string CatalogId)
    {
        DataTable dt = new DataTable();
        int d = 1;
        dt = BLL.CommonBLL.Select("PersonalizationCatalog", "*", " AND CatalogId = " + CatalogId + " AND OwnerId = '" + Common.RequestBase.GetQueryString("roleid").Trim() + "'");
        if (dt.Rows.Count > 0)
        {
            d = 16;
        }
        dt.Clear();
        dt.Dispose();
        return d;
    }
    #endregion

    #region 保存设置
    /// <summary>
    /// 保存设置
    /// </summary>
    /// <param name="arrCatalogid"></param>
    public void SaveSettings(string arrCatalogid)
    {
        string roleid = Common.RequestBase.GetQueryString("roleid");
        foreach (string list in arrCatalogid.Split('|'))
        {
            string[] arr = list.Split(';');
            if (arr[1].Trim() == "G")
            {
                BLL.SystemRoles.PersonalizationCatalogSettings(arr[0].Trim(), roleid, 1);
            }
            if (arr[1].Trim() == "X")
            {
                BLL.SystemRoles.PersonalizationCatalogSettings(arr[0].Trim(), roleid, 0);
            }
        }

        Response.Write("<script>alert(\"Successfully set!\");window.close();</script>");
    }
    #endregion
}
